﻿using Max.Ph5_2009A.NewShopping.Domain;
using Max.Ph5_2009A.NewShopping.DTO;
using Max.Ph5_2009A.NewShopping.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.Ph5_2009A.NewShopping.API.Controllers.Member
{
    /// <summary>
    /// 会员地址控制器
    /// </summary>
    [Route("member/address")]
    [ApiController]
    public class MemberAddressController : BaseController<MemberAddressModel>
    {
        IMemberAddressService _service;
        /// <summary>
        /// 构造函数（作用：初始化值）
        /// </summary>
        /// <param name="service"></param>
        public MemberAddressController(IMemberAddressService service) : base(service)
        {
            _service = service;
        }

        /// <summary>
        /// 查询该会员的所有收件人地址信息
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        [HttpGet("memberid")]
        public IActionResult Select(string memberId)
        {
            ResponseResultOutputDto dto = new();
            if (string.IsNullOrEmpty(memberId))
            {
                dto.Code = Domain.Shared.ResponseResultStateCode.系统错误;
                dto.Message = "参数无效";
                return BadRequest(dto);
            }

            var list = _service.Select().Where(x => x.MemberId.ToString().Equals(memberId)).OrderByDescending(x => x.IsDefault);
            dto.Data = list;

            return Ok(dto);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("dto")]
        public IActionResult Insert(MemberAddressInputDto model)
        {
            ResponseResultOutputDto dto = new();

            //判断参数是否有效
            if (!ModelState.IsValid)
            {
                dto.Code = Domain.Shared.ResponseResultStateCode.系统错误;
                dto.Message = "参数有误";
                return BadRequest(dto);
            }

            //调用业务
            int result = _service.Insert(model);

            if (result < 0)
            {
                dto.Code = Domain.Shared.ResponseResultStateCode.系统错误;
                dto.Message = Domain.Shared.ResponseResultStateCode.系统错误.ToString();
            }

            //返回值
            return Ok(dto);
        }
    }
}
